package pl.edu.agh.neuraleconomy.core.nn;

import java.util.Arrays;
import java.util.Collection;

import org.apache.log4j.Logger;
import org.encog.ml.data.MLDataSet;
import org.encog.util.arrayutil.TemporalWindowArray;

import pl.edu.agh.neuraleconomy.model.exchange.Exchange;

public class CoreUtils {
	
	@SuppressWarnings("unused")
	private static Logger logger = Logger.getLogger(CoreUtils.class);
	
	public static double[] toDoubleArray(Collection<Exchange> exchanges){
		double[] res = new double[exchanges.size()];
		
		int i = 0;
		for(Exchange e : exchanges){
			res[i++] = e.getClosingPrice().doubleValue();
		}
		
		return res;
	}
	
	public static MLDataSet createDataSet(double [] data, int inputLen, int outputLen){
//		if(data == null || inputLen + outputLen > data.length){
//			throw new IllegalArgumentException();
//		}
//		
//		int sets = data.length + 1 - inputLen - outputLen; 
//		double [][] input = new double[sets][];
//		double [][] output = new double [sets][];
//		
//		for(int i = 0; i < sets; i++){
//			input[i] = Arrays.copyOfRange(data, i, i + inputLen);
//			output[i] = Arrays.copyOfRange(data, i + inputLen, i + inputLen + outputLen);
//		}
//		
//		return new BasicMLDataSet(input, output);
		
		TemporalWindowArray window = new TemporalWindowArray(inputLen, outputLen);
		window.analyze(data);
		return window.process(data);
	}
	
	public static double[][] getTrainTestData(double [] data, double percent){
		double [][] res = new double [2][];
		
		int splitIndex = (int)((double) data.length * percent);
		
		res[0] = Arrays.copyOfRange(data, 0, splitIndex);
		res[1] = Arrays.copyOfRange(data, splitIndex, data.length);
		
		return res;
	}

}
